using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public abstract class EnemyAttack : MonoBehaviour
{
    // Start is called before the first frame update
    public float damage;
    public float starttime;
    public float continuetime;
    public float AttackTime;
    protected float AttackTimeKeeper;
    private Animator animator;
    private PolygonCollider2D collider2D;
    Transform transform;
    public int face = 1;
    Quaternion transformface;
    float deltadistance;
    GameObject player;
    Transform player_transform;
    protected EnemyBasic enemyBasic;
    private GameObject[] creations;
    public float StiffTime;
    public float RepelDistance;
    public int RepelTimes;

    public void Start()
    {
        animator = GetComponentInParent<Animator>();
        player = GameObject.FindWithTag("Player");
        player_transform = player.GetComponent<Transform>();
        //creation = GameObject.FindWithTag("Creation");
        enemyBasic = GetComponentInParent<EnemyBasic>();
        collider2D = GetComponent<PolygonCollider2D>();
        collider2D.enabled = false;
        transform = GetComponentInParent<Transform>();
        transformface = transform.localRotation;
        AttackTimeKeeper = AttackTime;
        deltadistance= GetComponentInParent<EnemyBasic>().deltadistance;
        face = 1;
        damage *= EnemyData.damageratio;
    }
    IEnumerator closecollider()
    {
        yield return new WaitForSeconds(continuetime);
        collider2D.enabled = false;
    }
    IEnumerator StartAttack()
    {
        yield return new WaitForSeconds(starttime);
        collider2D.enabled = true;
        StartCoroutine(closecollider());
    }
    protected void Attack()
    {
        if (player_transform.position.x + deltadistance >= transform.position.x&& player_transform.position.x - deltadistance <= transform.position.x)
        {
            animator.SetTrigger("attack");
            StartCoroutine(StartAttack());
            AttackTimeKeeper = AttackTime;
        }
        
    }
    protected void getplayer()
    {
        player_transform = player.GetComponent<Transform>();
        creations = GameObject.FindGameObjectsWithTag("Creation");
        foreach(GameObject creation in creations)
        {
            if (Mathf.Abs(creation.GetComponent<Transform>().position.x - transform.position.x) < Mathf.Abs(player_transform.position.x - transform.position.x))
            {
                player_transform = creation.GetComponent<Transform>();
            }
        }
    }
    // Update is called once per frame
    public void Update()
    {
        getplayer();
        if(enemyBasic.freeze==0)
            AttackTimeKeeper = AttackTime;
        if (AttackTimeKeeper < 0)
        {
                Attack();
        }
        else AttackTimeKeeper -= Time.deltaTime;
        //Debug.Log(enemyBasic.freeze);
    }
}
